home *** CD-ROM | disk | FTP | other *** search
- Path: abacus.abasoft.co.uk!not-for-mail
- From: dmb@abacus.abasoft.co.uk (David Byrne)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ problem with constructor.
- Date: 19 Mar 1996 15:13:34 -0000
- Organization: Abacus Software Ltd.
- Message-ID: <4imiuu$k3p@abacus.abasoft.co.uk>
- References: <DoGGGp.Muy@latcs1.lat.oz.au>
- NNTP-Posting-Host: abacus.abasoft.co.uk
- X-NNTP-Posting-Host: abacus.demon.co.uk
-
- In article <DoGGGp.Muy@latcs1.lat.oz.au>,
- Gregary J Boyles <boylesgj@lion.cs.latrobe.edu.au> wrote:
- >
- >int main()
- >{
- > Address Address1(56,"Derby Drive","Epping",3165);
- > Address Address2(22,"Claremont Street","Fawkner",3060);
- >
- > /********************************************************
- > After the above two calls Address1 and Address2 contain
- > all the appropriate data however after the next call all
- > 3 contain 0 or null strings. WHY?
- > ********************************************************/
-
- It's only by an act of God that it doesn't blow up sooner ! Your problem
- is that Address.Street and Address.City are uninitialised char*
- When Address::Address(...) is called, the (random) memory areas which are
- pointed to by these data members are trashed. See below.
- >
- > Address Address3(Address1);
- > return(0);
- >}
-
- >Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
- >{
- > Number=ANumber;
- > strcpy(Street,AStreet);
- > strcpy(City,ACity);
-
- You should do something
- like:
-
- Street = new char[30];
- strcpy(Street, AStreet);
- City = new char[30];
- strcpy(City, ACity);
-
- and same in Address::Address()
-
- > Zip=AZip;
- >}
-
- [snip]
-
- Regards,
-
- David
- --
- David Byrne, Abacus Software, London, UK Tel: +44 (0)171 603 9877
- Email: dmb@abacus.demon.co.uk Fax: +44 (0)171 603 6844
- Here's a koan: If you have ice-cream I will give you some. If you have none,
- I will take it away from you. (it's an ice-cream koan).
-